//
//  EditUserNameVC.m
//  KEREN
//
//  Created by MW on 2023/3/30.
//

#import "EditUserNameVC.h"

@interface EditUserNameVC ()
@property (nonatomic,strong)UITextField *textField;
@end

@implementation EditUserNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"保存" navColorIswhite:YES withTitle:@"修改昵称"];
    WEAKSELF
    self.rightClickBlock = ^(void){
        [weakSelf updateUserName];
        //User_update
    };
    [self setViewLayout];
}

- (void)updateUserName{
    WEAKSELF
    if(_textField.text.length > 0){
        [NetworkingTool POST:User_update parameters:@{@"name":_textField.text} isDeviceUrl:NO success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
            if([response[@"code"] intValue] == 0){
                
                NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] valueForKey:USERINFOKEY]];
                [dict setObject:self.textField.text forKey:@"name"];
                [[NSUserDefaults standardUserDefaults] setObject:dict forKey:USERINFOKEY];
                [weakSelf.navigationController popViewControllerAnimated:YES];

            }else{
                //[[KRAlartManager shareInstance] showErrorAlert:response[@"msg"]];
            }
        } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
            
        }];
    }else{
        [[KRAlartManager shareInstance] showErrorAlert:@"请输入新昵称"];
    }
}

- (void)setViewLayout{

    _textField = [UITextField setFlieldplaceholder:@"" Font:Font(16) borderColor:[UIColor clearColor]];
    _textField.frame = CGRectMake(0, NAVTOP_Height, DEVICE_WIDTH, 60);
    [self.view addSubview:_textField];
    _textField.layer.cornerRadius = 0;
    _textField.backgroundColor = Whire_Color;
    _textField.textColor = Color_333333;
    _textField.font = Font(16);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:_textField];

    UIButton *clearBtn = [UIButton setButtonWithImage:@"clear" cornerRadius:0 backColor:[UIColor clearColor]];
    [self.view addSubview:clearBtn];
    _textField.text = _userName;
    [clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.width.height.offset(21);
        make.centerY.equalTo(_textField);
    }];
    [UIView setLineViewWithframe:CGRectMake(24, 59, DEVICE_WIDTH - 48, 1) forinView:_textField];
    [clearBtn addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clear{
    _textField.text = @"";
}
-(void)textFiledEditChanged:(NSNotification*)notification{
    UITextField *textField = notification.object;
    NSString *str = textField.text;
    str = [self disable_emoji:str];
    if(str.length > 16 && textField.markedTextRange == nil){
        str =  [str substringToIndex:16];
        textField.text = str;
    }
}

- (NSString *)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}


@end
